﻿//------------------------------------------------------------------------------
// <copyright file="CommerceFoundationServiceAgent.cs" company="CommerceServer.net, Inc">
//    (c) 2012 CommerceServer.net, Inc. and its affiliates. All rights reserved.
// </copyright>
// <summary></summary>
//------------------------------------------------------------------------------ 
namespace MVCSample
{
    using CommerceServer.Foundation;
    using System;

    public static class CommerceFoundationServiceAgent
    {
        /// <summary>
        /// Sample channel name.
        /// </summary>
        public const string Channel = "";

        /// <summary>
        /// Service agent instance.
        /// </summary>
        private static OperationServiceAgent operationServiceAgent;

        /// <summary>
        /// Executes the specified operation.
        /// </summary>
        /// <param name="operation">The operation.</param>
        /// <returns>The response instance.</returns>
        public static CommerceResponse Execute(CommerceBaseBuilder operation)
        {
            ParameterChecker.CheckForNull(operation, "operation");

            if (operationServiceAgent == null)
            {
                operationServiceAgent = new OperationServiceAgent();
            }

            return operationServiceAgent.ProcessRequest(
                GetCurrentRequestContext(), operation.ToRequest());
        }

        /// <summary>
        /// Gets the current request context.
        /// </summary>
        /// <returns>A request context instance.</returns>
        private static CommerceRequestContext GetCurrentRequestContext()
        {
            CommerceRequestContext requestContext = new CommerceRequestContext()
            {
                Channel = Channel,
                UserLocale = "en-US",
                UserUILocale = "en-US",
                RequestId = Guid.NewGuid().ToString()
            };

            return requestContext;
        }
    }
}